<?php


namespace app\api\library\login\getPhone;


use app\api\help\Http;
use app\api\model\MallUser;
use app\api\model\MallWxapp;

class V
{
    public static function run($param)
    {
        $code        = $param[ 'code' ];
        $userId      = $param[ 'userId' ];
        $accessToken = MallWxapp::getAccessToken();
        $c           = Http::post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$accessToken", json_encode(['code' => $code]));
        $resArr      = json_decode($c, true);
        if (empty($resArr)) {
            _outPut(204, [], '获取用户电话失败');
        }
        if (isset($resArr[ 'errcode' ]) && $resArr[ 'errcode' ] !== 0) {
            _outPut(204, [], $resArr[ 'errmsg' ]);
        }
        if (empty($resArr[ 'phone_info' ])) {
            _outPut(204, [], '获取用户电话失败1');
        }
        $user    = MallUser::where('id', $userId)->find();
        $phone   = $resArr[ 'phone_info' ][ 'purePhoneNumber' ];
        $modData = ['wx_mobile' => $phone];
        if (empty($user[ 'mobile' ])) {
            $modData[ 'nick_name' ]  = 'ap_' . substr($phone, -4);
            $modData[ 'avatar_url' ] = 'https://tg.aipintuangou.com/upload/20230422/avatar.png';
            $modData[ 'mobile' ]     = $phone;
        }
        if (false === MallUser::where('id', $userId)->update($modData)) {
            
            _outPut(204, [], '获取用户电话失败2');
        }
        $user = MallUser::where('id', $userId)->find();
        _output(200, $user);
    }
}